package cpuSched;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;

import Main.FileReader;

import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Timer;

import model.CPU;
public class CPUPanel extends JPanel implements ActionListener{
	private FileReader fr;
	private LinkedList<CPU> cpus;
	private BufferedImage cpImg;
	private int x;
	private JScrollPane sp;
	
	public CPUPanel(FileReader fr){
		this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		this.fr=fr;
		
		cpus = new LinkedList<CPU>();
	//	setNoCPU(fr.getSystemConfiguration().get(0).getNoCPU());
		setNoCPU(10);
		for(CPU cp :cpus){
			System.out.print("CPU"+cp.getCpuId());
			System.out.println("curProcess"+cp.getCpuCurProcess());
		}
		
		try {
			cpImg= ImageIO.read(new File("images/cpu.png"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		x=15;
		this.setPreferredSize(new Dimension(cpus.size()*100, 100));
		
	}
	public void setSP(JScrollPane sp){
		this.sp=sp;
	
	}

	public void setNoCPU(int n){
		CPU cpu;
		for(int i=0; i<n; i++){
			cpu = new CPU();
			cpu.setCpuId(i); 
			cpus.add(cpu);
		}
		
	}
	public void setProcessCPU(int curCPU, int curProcess){
		cpus.get(curCPU).setCpuCurProcess(curProcess);
	
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		//repaint();
	}
	public void paint(Graphics g){
		x=15;
		super.paintComponent(g);
		Graphics2D g2D = (Graphics2D) g;
		g2D.setFont(new Font("Arial", Font.PLAIN, 30));
		g2D.setColor(Color.white);
		int i=0;
		sp.revalidate();
		for(CPU cp: cpus){
			g2D.drawImage(cpImg, x, 10, null);
			if(cpus.get(i).getCpuCurProcess()>0)
				g2D.drawString(cpus.get(i).getCpuCurProcess()+"", x+20, 5);
			x=x+100;
			i++;
		}
		
		
	}
}
